<template>
<div class="message-item" @click="$emit('click')">
    <!-- 头像/图标部分 -->
    <div class="item-avatar">
        <!-- 用户头像 -->
        <template v-if="type === 'user'">
            <img :src="avatar" alt="user-avatar" class="avatar-image" />
        </template>

        <!-- 系统通知图标 -->
        <template v-else-if="type === 'notice'">
            <div class="notice-icon">
                <van-icon :name="icon" size="24" :color="iconColor || '#fe496d'" />
            </div>
        </template>
    </div>

    <!-- 内容部分 -->
    <div class="item-content">
        <!-- 头部信息：标题和时间 -->
        <div class="item-header">
            <span class="item-title">{{ title }}</span>
            <span class="item-time">{{ time }}</span>
        </div>

        <!-- 主要内容 -->
        <div class="item-body">
            <div class="item-message">{{ message }}</div>
            <!-- 帖子预览 -->
            <div v-if="showPostPreview && postImage" class="post-preview" @click.stop="$emit('post-click')">
                <img :src="postImage" alt="post" class="post-image" />
                <div class="post-title">{{ postTitle }}</div>
            </div>
        </div>
    </div>

    <!-- 右侧操作区 -->
    <div v-if="showAction" class="item-action">
        <slot name="action">
            <!-- 默认操作按钮 -->
            <van-button v-if="type === 'user'" size="small" :type="actionStatus ? 'default' : 'primary'" round
                :class="{ 'followed-btn': actionStatus, 'follow-btn': !actionStatus }"
                @click.stop="$emit('action-click')">
                {{ actionStatus ? '已关注' : '关注' }}
            </van-button>
        </slot>
    </div>
</div>
</template>

<script>
export default {
    name: 'MessageItem',
    props: {
        // 消息类型: user(用户消息), notice(系统通知)
        type: {
            type: String,
            default: 'user',
            validator: val => ['user', 'notice'].includes(val)
        },
        // 头像URL或图标名称
        avatar: {
            type: String,
            default: ''
        },
        // 系统通知图标 (vant icon name)
        icon: {
            type: String,
            default: 'bell'
        },
        // 图标颜色
        iconColor: {
            type: String,
            default: ''
        },
        // 标题/用户名
        title: {
            type: String,
            required: true
        },
        // 时间
        time: {
            type: String,
            required: true
        },
        // 消息内容
        message: {
            type: String,
            default: ''
        },
        // 是否显示帖子预览
        showPostPreview: {
            type: Boolean,
            default: false
        },
        // 帖子图片
        postImage: {
            type: String,
            default: ''
        },
        // 帖子标题
        postTitle: {
            type: String,
            default: ''
        },
        // 是否显示右侧操作区
        showAction: {
            type: Boolean,
            default: false
        },
        // 操作状态 (例如:是否已关注)
        actionStatus: {
            type: Boolean,
            default: false
        }
    }
}
</script>

<style scoped>
.message-item {
    display: flex;
    padding: 16px 0;
    border-bottom: 1px solid #f5f5f5;
}

.message-item:last-child {
    border-bottom: none;
}

.item-avatar {
    width: 40px;
    height: 40px;
    margin-right: 12px;
    flex-shrink: 0;
}

.avatar-image {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.notice-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff5f7;
}

.item-content {
    flex: 1;
    overflow: hidden;
}

.item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.item-title {
    font-size: 15px;
    font-weight: 500;
    color: #333;
}

.item-time {
    font-size: 12px;
    color: #999;
}

.item-body {
    display: flex;
    flex-direction: column;
}

.item-message {
    font-size: 14px;
    color: #666;
    margin-bottom: 8px;
    line-height: 1.4;
}

.post-preview {
    display: flex;
    background-color: #f7f8fa;
    border-radius: 4px;
    padding: 8px;
    overflow: hidden;
}

.post-image {
    width: 60px;
    height: 60px;
    border-radius: 4px;
    object-fit: cover;
    margin-right: 10px;
}

.post-title {
    flex: 1;
    font-size: 13px;
    color: #666;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.item-action {
    padding-left: 12px;
    display: flex;
    align-items: center;
}

.follow-btn {
    background-color: #fe496d;
    border: none;
    color: #fff;
}

.followed-btn {
    background-color: #f5f5f5;
    border: none;
    color: #666;
}
</style>